package com.gwl.autoqq.controller;

import com.gwl.autoqq.pojo.RespDict;
import com.gwl.autoqq.pojo.ResponseResult;
import com.gwl.autoqq.pojo.ToQQCrowdBean;
import com.gwl.autoqq.pojo.UnSignStudentVo;
import com.gwl.autoqq.pojo.enums.AppHttpCodeEnum;
import com.gwl.autoqq.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.atomic.AtomicInteger;

@Controller
@ResponseBody
@RequestMapping("/autoqq")
@Slf4j
public class MsgController {
    @Autowired
    Utils utils;

    // 发送私人qq
    @PostMapping("/sendPrivate")
    public ResponseResult sendMsg(@RequestBody RespDict respDict){
        AppHttpCodeEnum sendMsgFlag = utils.sendMsg(respDict);
        return ResponseResult.setAppHttpCodeEnum(sendMsgFlag);
    }

    /**
     * 发送带格式的格式的接口
     * @param toQQCrowdBean
     * @return
     */
    @PostMapping("/receiveCatInfo")
    public ResponseResult receiveCatInfo(@RequestBody ToQQCrowdBean<UnSignStudentVo> toQQCrowdBean){
        log.info("接受到的信息: " + toQQCrowdBean.toString());
        if(!toQQCrowdBean.getCode().equals(200))
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_CROWD);
        RespDict respDict = new RespDict();
        respDict.setMsgType(toQQCrowdBean.getType());
        respDict.setNumber(toQQCrowdBean.getQqCrowd());
        StringBuilder unSignStudentMsg = new StringBuilder();
        unSignStudentMsg.append(toQQCrowdBean.getDatetime() + "未打卡名单\n");
        AtomicInteger sum = new AtomicInteger(1);
        toQQCrowdBean.getData().forEach( unSignStudent -> {
            unSignStudentMsg.append(sum + ":" + unSignStudent.getStuName());
            unSignStudentMsg.append("," + unSignStudent.getStuNum());
            unSignStudentMsg.append("," + unSignStudent.getMobile() + "\n");
            sum.getAndIncrement();
        });
        respDict.setMsg(unSignStudentMsg.toString());
        utils.sendMsg(respDict);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 由app端推送的信息格式
     * @param toQQCrowdBean
     * @return
     */
    @PostMapping("/receiveCatInfoTmp")
    public ResponseResult receiveCatInfoTmp(@RequestBody ToQQCrowdBean<UnSignStudentVo> toQQCrowdBean){
        log.info("接受到的信息: " + toQQCrowdBean.toString());
        if(!toQQCrowdBean.getCode().equals(200))
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_CROWD);
        RespDict respDict = new RespDict();
        respDict.setMsgType(toQQCrowdBean.getType());
        respDict.setNumber(toQQCrowdBean.getQqCrowd());
        respDict.setMsg(toQQCrowdBean.getData().toString()); // 将app的数据直线显示
        utils.sendMsg(respDict);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
